package enumdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-10-10
 * Time: 17:50
 */
public class Test {
    public static void main(String[] args) {
        try {
            Class<?> c1 = Class.forName("enumdemo.TestEnum");
            Constructor<?> constructor =
                    c1.getDeclaredConstructor(String.class, int.class, int.class, String.class);
            constructor.setAccessible(true);

            TestEnum testEnum =
                    (TestEnum) constructor.newInstance(9, "WHITEBLACK");
            System.out.println(testEnum);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射私有的构造方法，屏蔽内容为获取共有的构造方法
    public static void main1(String[] args) {
        try {
            Class<?> c = Class.forName("enumdemo.TestEnum");
            Constructor<?> constructor = c.getDeclaredConstructor(String.class, int.class, int.class, String.class);
            constructor.setAccessible(true);
            TestEnum testEnum = (TestEnum) constructor.newInstance(10, "WHILEBLACK");
            System.out.println(testEnum);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
